﻿using System;
using System.Text;
using AwesomeEnginePC.Physics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace AwesomeEnginePC.Graphics.TwoDimensional.Text
{
    public class BaseString : Base2DObject
    {
        private SpriteFont _fontSprite;
        public BaseString(ContentManager CM, string assetName, string text)
            : base(CM)
        {
            Text = text;
            _fontSprite = LoadContent<SpriteFont>(assetName);
        }

        public BaseString(ContentManager CM, string assetName, string text, Vector<int> position)
            : base(CM)
        {
            Text = text;
            _fontSprite = LoadContent<SpriteFont>(assetName);
        }

        public BaseString(ContentManager CM, string assetName, string text, Vector<int> position, Color tint)
            : base(CM)
        {
            Text = text;
            Position = position;
            Tint = tint;
            _fontSprite = LoadContent<SpriteFont>(assetName);
        }

        public BaseString(ContentManager CM, string assetName, string text, Vector<int> position, Color tint, Vector<float> scaling, float rotation)
            : base(CM)
        {
            Text = text;
            Position = position;
            Tint = tint;
            Scaling = new Vector<float>(scaling.X, scaling.Y);
            Rotation = rotation;
            _fontSprite = LoadContent<SpriteFont>(assetName);
        }

        public SpriteFont FontSprite
        {
            get { return _fontSprite; }
        }

        public override int RawHeight
        {
            get { return (int)_fontSprite.MeasureString(Text).Y; }
        }

        public override int RawWidth
        {
            get { return (int)_fontSprite.MeasureString(Text).X; }
        }

        public string Text { get; set; }

        /// <summary>
        /// This Function Parses a string to wrap text based on a max word count
        /// </summary>
        /// <param name="maxLineWordCount">Max Amount of words before a New Line</param>
        /// <returns>Returns the New String</returns>
        public string WordWrapText(float maxLineWordCount)
        {
            string[] words = Text.Split(' ');
            StringBuilder strbuilder = new StringBuilder();
            int count = 1;
            float spaceWidth = FontSprite.MeasureString(" ").X;
            for (int i = 0; i < words.Length; i++)
            {
                Vector2 size = FontSprite.MeasureString(words[i]);

                if (count < maxLineWordCount)
                {
                    strbuilder.Append(words[i] + " ");
                    count++;
                }
                else
                {
                    strbuilder.Append("\n" + words[i] + " ");
                    count = 1;
                }
            }

            return strbuilder.ToString();
        }

        public override void Update(GameTime gameTime)
        {
        }

        protected override void DrawInner(GameTime gameTime, SpriteBatch SB)
        {
            SB.DrawString(_fontSprite, Text, Position.ToVector2(), Tint, Rotation, Origin.ToVector2(), Scaling.ToVector2(), SpriteEffects.None, LayerDepth);
        }
    }
}